#!/bin/bash
# vim: fdm=marker fmr=[[[,]]] cms=\ #%s

[[ -z $SOAR ]] && SOAR=/opt/soar
PATH=$PATH:$SOAR/common/bash/soar
. soar.sh

usage()
{ #[[[
    echo "Usage: $(basename $0) {server}"
} #]]]

main()
{ #[[[
    if [[ -z $SVCENV_DIR ]]
    then
        echo "Error: SVCENV_DIR not set"
        return 1
    fi
    if [[ -z $SVCENV_CNFG ]]
    then
        echo "Error: SVCENV_CNFG not set"
        return 1
    fi
    local svr=svr_$1
    if [[ $svr == "svr_" ]]
    then
        usage
        return 1
    fi

    local svr_cnfg=$(cnfg_get_property "$SVCENV_CNFG" SERVER $svr)
    if [[ -z $svr_cnfg ]]
    then
        echo "Error: no $svr in \$SVCENV_CNFG"
        return 1
    fi

    local running

    running=$(process_is_running $svr)
    if (( running ))
    then
        echo "$svr is still running... skip"
        return 0
    fi

    cd $SVCENV_DIR/bin
    if [[ ! -x $svr ]]
    then
        echo "Error: $svr not in \$SVCENV_DIR/bin or not executable"
        return 1
    fi
    PATH=.:$PATH
    $svr
    sleep 1

    running=$(process_is_running $svr)
    if (( running ))
    then
        echo "$svr start succeed"
        return 0
    else
        echo "$svr start fail"
        return 1
    fi
} #]]]

main $@
exit $?
